<?php
namespace Modules\Admin\Controllers;

class UserWalletGoodsController extends ControllerBase {
  public function indexAction() {
    $request = $this -> request;

    $currentPage = abs($request -> getQuery('page', 'int', 1));
    if($currentPage <= 0) {
      $currentPage = 1;
    }

    $userWalletGoodsLogBuilder = $this -> modelsManager -> createBuilder()
      -> from([
        'u' => 'Common\\Models\\UserModel',
        'uwgl' => 'Common\\Models\\UserWalletGoodsLogModel',
      ])
      -> columns(
        'u.user_id, u.user_code, '.
        'uwgl.type, uwgl.active, uwgl.create_time, uwgl.goods, uwgl.goods_new, uwgl.goods_old, uwgl.poundage'
      )
      -> where('u.user_id = uwgl.user_id AND uwgl.is_draw = 1')
      -> orderBy('uwgl.user_wallet_goods_log_id DESC');

    $userCode = $request -> getQuery('userCode', 'string', false, true);
    if($userCode) {
      $userWalletGoodsLogBuilder -> andWhere('user_code = ' . $userCode);
    }

    $userWalletGoodsLogPage = new \Phalcon\Paginator\Adapter\QueryBuilder([
      'builder' => $userWalletGoodsLogBuilder,
      'limit' => 10,
      'page' => $currentPage
    ]);

    return $this -> response -> setJsonContent([
      'code' => 0,
      'data' => ['user_wallet_goods_log' => $userWalletGoodsLogPage -> getPaginate()]
    ]);
  }
}
?>